package com.knight.jdk8.stream;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Description:
 * @author: knight
 * @Date: 2019-05-12 21:00
 */
public class StreamTest11 {
    public static void main(String[] args) {
        Student student1 = new Student("zhangsan", 20, 100);
        Student student2 = new Student("lisi", 20, 90);
        Student student3 = new Student("wangwu", 30, 90);
        Student student4 = new Student("zhangsan", 40, 80);
        Student student5 = new Student("zhaoliu", 30, 90);

        List<Student> students = Arrays.asList(student1, student2, student3, student4);

        // 查询成绩最低的学生
        students.stream().min(Comparator.comparingInt(Student::getScore)).ifPresent(System.out::println);
        System.out.println("----------------");
        // 查询最高成绩
        students.stream().max(Comparator.comparingInt(Student::getScore)).ifPresent(System.out::println);
        // 查询平均成绩
        System.out.println("----------------");
        System.out.println(students.stream().collect(Collectors.averagingInt(Student::getScore)));
        // 查询总成绩
        System.out.println("----------------");
        System.out.println(students.stream().mapToInt(Student::getScore).sum());
        //概况
        IntSummaryStatistics statistics = students.stream().collect(Collectors.summarizingInt(Student::getScore));
        System.out.println(statistics);
        System.out.println("----------------");
        //将学生的姓名拼接并通过逗号分隔
        System.out.println(students.stream().map(Student::getName).collect(Collectors.joining(",")));
        System.out.println("--------------------");
        //两次分组 先按照分数分组再按照年龄分组
        Map<Integer, Map<Integer, List<Student>>> collect = students.stream().collect(Collectors.groupingBy(Student::getScore, Collectors.groupingBy(Student::getAge)));
        System.out.println(collect);

        System.out.println("-----------------");

    }
}